Skip to content

Revert schema object reuse#3469

Merged
koxudaxi merged 1 commit into
mainfrom
revert-schema-cache
Jun 21, 2026
Merged

Revert schema object reuse#3469
koxudaxi merged 1 commit into
mainfrom
revert-schema-cache

Conversation

@koxudaxi

@koxudaxi koxudaxi commented Jun 21, 2026

Copy link
Copy Markdown
Owner

Summary by CodeRabbit

  • Refactor
    • Simplified JSON Schema parser's internal reference resolution mechanism for improved maintainability

@coderabbitai

coderabbitai Bot commented Jun 21, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: c217dcaf-c53f-4ff7-bab7-4d39eaf89f45

📥 Commits

Reviewing files that changed from the base of the PR and between d71d633 and 75d305f.

⛔ Files ignored due to path filters (1)
  • tests/data/jsonschema/same_file_ref_fast_path.json is excluded by !tests/data/**/*.json and included by none
📒 Files selected for processing (4)
  • src/datamodel_code_generator/parser/jsonschema.py
  • tests/data/expected/main/jsonschema/same_file_ref_fast_path.py
  • tests/main/jsonschema/test_main_jsonschema.py
  • tests/parser/test_jsonschema.py
💤 Files with no reviewable changes (3)
  • tests/data/expected/main/jsonschema/same_file_ref_fast_path.py
  • tests/main/jsonschema/test_main_jsonschema.py
  • tests/parser/test_jsonschema.py

📝 Walkthrough

Walkthrough

JsonSchemaParser drops its _ref_schema_object_cache dict entirely. _load_ref_schema_object() is rewritten to resolve refs directly via _get_ref_body, split_json_pointer, and get_model_by_path on every call. The corresponding cache pre-population in _parse_file is removed, and all tests and expected fixtures that verified the caching behavior are deleted.

Changes

Remove $ref schema object cache

Layer / File(s) Summary
Remove _ref_schema_object_cache from parser
src/datamodel_code_generator/parser/jsonschema.py
Removes the cache attribute from __init__, rewrites _load_ref_schema_object() to resolve refs directly through _get_ref_body + fragment traversal on every call, and removes definition pre-population of the cache in _parse_file.
Delete caching tests and expected fixtures
tests/parser/test_jsonschema.py, tests/main/jsonschema/test_main_jsonschema.py, tests/data/expected/main/jsonschema/same_file_ref_fast_path.py
Removes CountingJsonSchemaParser, RefBodyCountingJsonSchemaParser, four unit tests asserting cache hit/reuse semantics, test_main_same_file_ref_fast_path, and the expected Pydantic output file for the fast-path scenario.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • koxudaxi/datamodel-code-generator#3466: Adds _ref_schema_object_cache caching and pre-validation reuse for _load_ref_schema_object and _parse_file definitions — the exact inverse of this PR's removal.
  • koxudaxi/datamodel-code-generator#3467: Changes JsonSchemaParser._load_ref_schema_object() and _parse_file() definition validation around _ref_schema_object_cache, overlapping directly with the same methods modified here.
  • koxudaxi/datamodel-code-generator#3445: Reworks $ref resolution and definition iteration in jsonschema.py, including parse_obj path detection for "$ref": "#", overlapping with the ref-resolution path rewritten in _load_ref_schema_object.

Poem

🐇 The cache has hopped away today,
no stale refs stored to lead astray.
Each $ref now freshly found,
through pointers traced on solid ground.
Simpler paths, the rabbit says —
less to carry through the maze!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Revert schema object reuse' directly and concisely describes the main change: removal of the internal per-$ref cache mechanism for JsonSchemaObject instances and related caching logic.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch revert-schema-cache

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions

Copy link
Copy Markdown
Contributor

📚 Docs Preview: https://pr-3469.datamodel-code-generator.pages.dev

@codspeed-hq

codspeed-hq Bot commented Jun 21, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 12.76%

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 11 regressed benchmarks
⏩ 98 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
WallTime test_perf_deep_nested 4.3 s 5.1 s -14.32%
WallTime test_perf_duplicate_names 892.8 ms 1,037.8 ms -13.98%
WallTime test_perf_kubernetes_style_pydantic_v2 2.2 s 2.5 s -13.6%
WallTime test_perf_all_options_enabled 4.5 s 5.3 s -13.59%
WallTime test_perf_multiple_files_input 3 s 3.5 s -13.57%
WallTime test_perf_stripe_style_pydantic_v2 1.7 s 1.9 s -12.99%
WallTime test_perf_large_models_pydantic_v2 3.1 s 3.5 s -12.8%
WallTime test_perf_complex_refs 1.7 s 1.9 s -11.78%
WallTime test_perf_graphql_style_pydantic_v2 686 ms 775.7 ms -11.57%
WallTime test_perf_aws_style_openapi_pydantic_v2 1.6 s 1.8 s -11.32%
WallTime test_perf_openapi_large 2.5 s 2.8 s -10.76%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing revert-schema-cache (75d305f) with main (d71d633)

Open in CodSpeed

Footnotes

  1. 98 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@codecov

codecov Bot commented Jun 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (d71d633) to head (75d305f).

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #3469   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          149       149           
  Lines        32276     32207   -69     
  Branches      3772      3769    -3     
=========================================
- Hits         32276     32207   -69     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@koxudaxi koxudaxi merged commit 19039d0 into main Jun 21, 2026
61 of 62 checks passed
@koxudaxi koxudaxi deleted the revert-schema-cache branch June 21, 2026 18:13
@github-actions

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: PR #3469 reverts an internal schema-object reuse/caching optimization in the JSON Schema parser. All affected symbols are private implementation details: the _ref_schema_object_cache attribute, the private _get_ref_raw_doc method, and internal logic within the private _load_ref_schema_object method. None are part of the public Python API or CLI — no command-line options, public classes, enum values, function signatures, or default values are removed or changed in the diff. The reverted optimization was explicitly output-neutral (the deleted test asserted same-file refs keep generated output unchanged), so reverting it does not alter generated code output, templates, error handling, or supported Python versions. The remaining deletions are test files and test fixtures. Therefore this PR contains no breaking changes.


This analysis was performed by Claude Code Action

@github-actions

Copy link
Copy Markdown
Contributor

🎉 Released in 0.65.0

This PR is now available in the latest release. See the release notes for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant